/**
 * 
 */
package leetCode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author zhong
 *
 */
public class FindAnagramMappings {
	public int[] anagramMappings(int[] A, int[] B) {
		int n = A.length;
		int[] P = new int[n];
		HashMap<Integer, List<Integer>> val2Idx = new HashMap<>(n);
		for (int i = 0; i < P.length; i++) {
			if (val2Idx.containsKey(B[i])) {
				val2Idx.get(B[i]).add(i);
			} else {
				List<Integer> list = new ArrayList<>();
				list.add(i);
				val2Idx.put(B[i], list);
			}
		}
		for (int i = 0; i < P.length; i++) {
			List<Integer> list = val2Idx.get(A[i]);
			int index = list.size() - 1;
			P[i] = list.remove(index);
		}
		return P;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] A = { 12, 28, 46, 32, 50 };
		int[] B = { 50, 12, 32, 46, 28 };
		System.out.println(new FindAnagramMappings().anagramMappings(A, B));
	}

}
